package com.msb.mall.product.controller;

import com.msb.common.utils.PageUtils;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.entity.product.CategoryEntity;
import com.msb.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author leixu
 * @email xuleiwolf@163.com
 * @date 2024-04-19 20:08:58
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/findCategoryWithTree")
    public CommonResult findCategoryWithTree() {
        List<CategoryEntity> list = categoryService.findCategoryWithTree();

        return CommonResult.success(null, list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public CommonResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return CommonResult.success(null, page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    public CommonResult info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return CommonResult.success(null, category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public CommonResult addCategorySingle(@RequestBody CategoryEntity category) {
        return categoryService.addCategorySingle(category);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody CategoryEntity category) {
        return categoryService.updateCategory(category);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public CommonResult delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));

        return CommonResult.success();
    }

}
